package com.crm.controller.system;

import cn.hutool.core.util.ArrayUtil;
import com.crm.annotation.Log;
import com.crm.base.BusinessType;
import com.crm.base.OperatorType;
import com.crm.base.ResponseResult;
import com.crm.domain.entity.Dept;
import com.crm.domain.vo.TreeItemVo;
import com.crm.service.IDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Xanny,Hulunico
 */
@RestController
@Api(tags = "部门管理")
@RequiredArgsConstructor
@RequestMapping("/system/dept")
public class DeptController {
    private final IDeptService deptService;

    /**
     * 查询所有部门
     * @return
     */
    @ApiOperation("部门列表")
    @GetMapping("/list")
    public ResponseResult list(){
        List<Dept> list = deptService.list();
        return ResponseResult.success(list);
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @ApiOperation("添加部门")
    @PostMapping
    @Log(title = "操作日志",businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE)
    public ResponseResult addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        return ResponseResult.success();
    }

    /**
     * getById
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseResult getById(@PathVariable Long id){
        Dept dept = deptService.lambdaQuery().eq(Dept::getDeptId, id).one();
        return ResponseResult.success(dept);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @GetMapping("list/exclude/{id}")
    public ResponseResult getExclude(@PathVariable Long id){
        //拿到所有的dept
        List<Dept> deptList = deptService.lambdaQuery().list();

        deptList.removeIf(dept -> (dept.getDeptId().equals(id)|| ArrayUtil.contains(StringUtils.split(dept.getAncestors(),","),dept + "")));

        return ResponseResult.success(deptList);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ResponseResult deleteById(@PathVariable Long id){
        //如果该节点下有子节点 不可删除
        if(deptService.lambdaQuery().eq(Dept::getParentId, id).count()>0){
          return ResponseResult.error("该部门存在下级部门，不可删除");
        }
        // TODO 如果该部门存在用户，不可删除
        if(false){
            return ResponseResult.error("该部门存在用户，不可删除");
        }
        deptService.lambdaUpdate().eq(Dept::getDeptId, id).remove();
        return ResponseResult.success();
    }

    /**
     * 获取部门下拉树列表
     * @return
     */
    @GetMapping("/treeselect")
    @ApiOperation("获取部门下拉树列表")
    public ResponseResult treeSelect(){
        List<TreeItemVo> treeVo = deptService.treeSelect();
        return ResponseResult.success(treeVo);
    }

    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/roleDeptTreeselect/{id}")
    public ResponseResult roleDeptTreeSelect(@PathVariable Long id){
        List<TreeItemVo> treeVo = deptService.treeSelect();
        return ResponseResult.success(treeVo);
    }
}
